﻿#include "stdafx.h"
#include "XEditIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "Util.h"
#include "IXEdit.h"

#include "ParseHelper.h"


CXEditIntParser::CXEditIntParser() : CXIntParserBase(&TIntCreateControl<IXEdit>)
{
    ;
}

CXEditIntParser::~CXEditIntParser()
{
    ;
}

// 创建控件之前
BOOL CXEditIntParser::PreCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    if(!m_ParentParser.PreCreate(pNode, pControl))
        return FALSE;

    BOOL bTemp;
    XBSTR bstrTemp;

    IXEdit* pEdit = Util::QueryIntf<IXEdit>(pControl);
    if(pEdit == NULL)
        return FALSE;

    // Style
    if(SUCCEEDED(pNode->GetValue(_T("style"), &bstrTemp)))
    {
        struct stStyleInfo
        {
            LPCTSTR szStyleName;
            DWORD   dwStyle;
        } data[] =
        {
            _T("password"),     ES_PASSWORD,
            _T("number"),       ES_NUMBER,
        };
        XString strStyle(bstrTemp);
        size_t handle = 0;
        XString strToken;
        while(StrUtil::NextToken(strStyle, handle, _T(";"), strToken, FALSE))
        {
            for(int i=0; i<_countof(data); ++ i)
            {
                if(_tcsicmp(data[i].szStyleName, strToken.GetData()) == 0)
                {
                    pEdit->ModifyStyle(data[i].dwStyle, 0);
                    break;
                }
            }
        }
    }

    // Multiline
    if(SUCCEEDED(pNode->GetValue(_T("multiline"), &bTemp)))
    {
        pEdit->put_Multiline(bTemp);
    }

    // autoscroll
    if(SUCCEEDED(pNode->GetValue(_T("autoscroll"), &bstrTemp)))
    {
        if(_tcsstr(bstrTemp, _T("horz")) != NULL)
            pEdit->put_AutoHScroll(bTemp);
        if(_tcsstr(bstrTemp, _T("vert")) != NULL)
            pEdit->put_AutoVScroll(bTemp);
    }
    return TRUE;
}

// 填充属性
BOOL CXEditIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    DWORD dwTemp;
    BOOL bTemp;
    COLORREF clrTemp;

    IXEdit* pEdit = Util::QueryIntf<IXEdit>(pControl);
    if(pEdit == NULL)
        return FALSE;

    // MaxCount
    if(SUCCEEDED(pNode->GetValue(_T("maxCount"), &dwTemp)))
    {
        pEdit->put_MaxCount(dwTemp);
    }

    // ReadOnly
    if(SUCCEEDED(pNode->GetValue(_T("readOnly"), &bTemp)))
    {
        pEdit->put_ReadOnly(bTemp);
    }

    // HintText
    if(SUCCEEDED(pNode->GetValue(_T("hintText"), &bstrTemp)))
    {
        XString strTemp(bstrTemp);
        if(strTemp.size() > 0 && strTemp.at(0) == _T('@'))
            strTemp = Util::LoadString(strTemp);
        pEdit->put_HintText(strTemp.GetData());
    }

    // HintTextColor
    if(SUCCEEDED(pNode->GetValue(_T("hintTextColor"), &clrTemp)))
    {
        pEdit->put_HintTextColor(clrTemp);
    }

    return bResult;
}
